import java.util.Iterator;
import java.util.NoSuchElementException;

public class TextFileIterator implements Iterator<String>{
	

	private String 	text;
	private String nextWord;
	
	public TextFileIterator(Resource res){
		text = getAsString(res);
		
		text = text.replaceAll("-\n(?=[a-z])", "");
		
		nextWord = first();
	}
	
	private String first() {
		int beginIdx = 0;

		while (beginIdx < text.length() && !Character.isLetter(text.charAt(beginIdx))
				&& !Character.isDigit(text.charAt(beginIdx))) {
			beginIdx++;
		}

		int idx = beginIdx;

		while (idx < text.length()
				&& (Character.isLetter(text.charAt(idx)) || Character.isDigit(text.charAt(idx)))) {
			idx++;
		}

		if (beginIdx == idx) {
			return null;
		}

		String word = text.substring(beginIdx, idx);
		text = text.substring(idx);
		return word;
	}

	public String getAsString(Resource res){
		return ("Viel Erfolg in dieser Klausur ;-)");
	}

	@Override
	public boolean hasNext() {
		// TODO Auto-generated method stub
		return nextWord!=null;
	}

	@Override
	public String next() {
		if (nextWord == null) {
			throw new NoSuchElementException();
		}
		
		String temp = nextWord;
		nextWord = first();
		return temp;
	}

	@Override
	public void remove() {
		throw new UnsupportedOperationException();
	}
	
}
